package com.jms.web.controller.jms.service.impl;
//import com.hsoft.sfrz.common.security.client.TysfrzClient4;
import org.springframework.util.DigestUtils;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
public class zuzhuang {
    public static void main(String[] args) throws Exception {
//        Map<String, String> params  = new HashMap<>();
//        String str = "{ticket:'9b1a57d064a9451783a781bf12e5b3b5'}";
//        String a = TysfrzClient4.encrypt(str,"439c1111eae84170a34ffb3bda28f997");
//        //System.out.println("加密："+a);
//        String b = "0058c94026119f3ec09ab2006d3298aa659c4b040e2346260ea9fb97e842b17579ed0bd192c08046dacca133028bdf4ede705ce1a16a108d3167e9ec05a798f135d32f2b6ae46a124e44c04400f5986833621cae9cf0a1b222fd94807ae68794fe4bdb57b1b2842f44b457d76eb579f62d31f1720d01d09d18877c684eb2a6cee31901397a494226e7709468de86539c758f7c5d199ea9b9b0d0b6923b98a0f6fed7ad940a2723d6053b73c73ccd3a8b42f967bb9ab09910602d8b9e8ac85b339c25e798cf57012e6f2251d3282519dbd0f9cfeb5d6eab92e8c1f745e2f18c10f47b394172c6b120649ca1e4200f252d479d7177e277a30922598a9068b6c01aa84b9dc675a4e8299aa030d357a2ed19d11b85b70c7ff03d53ba51b39d9012ea0918646321dda65c4bae3e8275777e8a7d8e2bf7ed012c569d42b2d107f9b0a1e4f91832ec7a2b55d77affc90903d0b3c8dadf623caa05d92e17800c21242f2b9a3bbafd7dfeabfead6d97b5db667ef5a7cf100318fb4056e05ece40aaf378db930b05c0bd279135889359b19b719d9459dc6dc787b938e3994fc20585f9fa765a21c45a35463732a921f0145083601f952d188d6ed014691d4ae30c3bde2b0c91a45bf86524144f729d1029477911ae65b0244e53befccee030c3384f5fe9e9f5d46d400f428498e8ea457c0f1e060fc716c00055229145a174a9f1fd59936978e5447f905003afca8da2dc219d003b9aca98e5c5649a6aed2384d0a25f5f96";
//       String users =  TysfrzClient4.decrypt(b,"439c1111eae84170a34ffb3bda28f997");
//        System.out.println("解密："+users);
//        getQueryUrl(params);
    }

    /**
     * 获得query参数，没有传空集合
     *
     * @param params
     * @return
     */
    public static String getQueryUrl(Map<String, String> params) {
        Map<String, String> sorted = new TreeMap<>();
        if (null != params && params.size() > 0) {
            params.forEach((k, v) -> sorted.put(k, v));
        }
// 海算平台上创建应用的AppKey
        sorted.put("AccessKey","ae4d5e023f3d4adfb12de751b637d62c");
// 海算平台上创建应用的AppSecret
        sorted.put("SecretKey", "aedcd8c697381c4c0873636cd6e115d4");
// 时间戳
        sorted.put("timestamp", String.valueOf(System.currentTimeMillis()));
// 随机数
        Random random = new Random();
        sorted.put("nonce", String.valueOf(random.nextInt()));
// Query位置的业务参数也需要纳入计算
// sorted.put("xxx", “xxx”);
// 最后计算签名
        sorted.put("sign", getSign(sorted));
        sorted.remove("SecretKey");
        StringBuilder builder = new StringBuilder();
        sorted.forEach((k, v) -> {
            try {
                builder.append(k).append("=").append(URLEncoder.encode(v, "utf-8")).append("&");
            } catch (UnsupportedEncodingException e) {
                System.out.println("对参数进行重新编码时出错。");
            }
        });
        builder.deleteCharAt(builder.length()-1);
        System.out.println(builder.toString());
        return builder.toString();
    }
    //计算sign
    public static String getSign(Map<String, String> params) {
        Map<String, String> sorted = new TreeMap<>();
        params.forEach((k, v) -> sorted.put(k, v));
        StringBuilder builder = new StringBuilder();
        sorted.forEach((k, v) -> {
            builder.append(k).append("=").append(v).append("&");
        });
        String value = builder.toString();
        value = value.substring(0, value.length() - 1);
        return DigestUtils.md5DigestAsHex(value.getBytes()).toUpperCase();
    }
}
